package com.google.code.head2head.gwtmodules.client.fileupload;

import com.google.code.head2head.gwtmodules.client.services.FacadeServicesAsync;
import com.google.code.head2head.gwtmodules.client.util.ServiceLocator;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.QuickTips;
import com.gwtext.client.widgets.event.ButtonListener;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormConfig;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.form.TextFieldConfig;

public class EntryInfoPanel extends VerticalPanel {

	private Form entryInfoForm;

	private TextField keyWordsField = null;

	private TextField titleField = null;

	public EntryInfoPanel() {
		initWidget();
	}

	private void initWidget() {

		add(new HTML("</br>2) Enter information on your entry</br>"));
		Field.setMsgTarget("side");
		QuickTips.init();
		entryInfoForm = new Form(new FormConfig() {
			{
				setHeader("Entry information form");
				setSurroundWithBox(true);
			}

		});

		titleField = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Title");
				setAllowBlank(false);
				setName("title");
				// setRegex("([a-z]*\t){10}");
				// setRegexText("Must be text separated by spaces");
				setWidth(150);
			}
		});
		entryInfoForm.add(titleField);

		keyWordsField = new TextField(new TextFieldConfig() {
			{
				setFieldLabel("Keywords");
				setAllowBlank(true);
				setName("keywords");
				// setRegex("([a-z]*\t){10}");
				// setRegexText("Must be text separated by spaces");
				setWidth(150);
			}
		});
		entryInfoForm.add(keyWordsField);

		Button submitButton = new Button("submit");
		submitButton.addButtonListener(new ButtonListener() {

			public void onClick(Button button, EventObject e) {
				saveInfo(titleField.getValueAsString(), keyWordsField
						.getValueAsString());
			}

			public void onMouseOut(Button button, EventObject e) {
				// TODO Auto-generated method stub

			}

			public void onMouseOver(Button button, EventObject e) {
				// TODO Auto-generated method stub

			}

			public void onToggle(Button button, boolean pressed) {
				// TODO Auto-generated method stub

			}

		});
		entryInfoForm.addButton(submitButton);
		entryInfoForm.render();
		this.add(entryInfoForm);

	}

	private void saveInfo(final String title, final String keywords) {

		FacadeServicesAsync svc = ServiceLocator.getFacadeServicesAsync();

		AsyncCallback callback = new AsyncCallback() {
			public void onSuccess(Object result) {
				clear();
				add(new HTML("Thank you for creating your own entry"));
				MessageBox.alert("Message", result.toString());
			}

			public void onFailure(Throwable ex) {
				MessageBox.alert("Message", "Problem saving entry information");
			}
		};
		// execute the service
		svc.addEntryInformations(title, keywords, callback);
	}

}
